<?php

class Dispatcher {

    private $_request = null;

    private $_response = null;

    private $_arg = null;

    private $_error = false;

    function __construct( $arg ) {
        $this->_arg = $arg;
        $this->_request = new Request( $this->_arg->get( Arg::PARAM ) );
        $this->_response = new Response();
    }

    public function dispatch() {
        Logs::Debug( var_export( $this->_request, true ) );
        
        // $this->_response = new Response();
        // $this->_response->PROTO_RESULT = 1001;
        // $this->_response->returnJson();
        
        if( $this->_error ) {
            return ;
        }
    }

    public function checkVersion() {
        if( $this->_error ) {
            return ;
        }
        
        $versionServer = Env::Ins()->version();
        $versionClient = $this->_request->PROTO_VERSION;
        if ( $versionClient < $versionServer ) {
            $this->_responseError( E_PROTO_RESULT::CLIENT_INVALID_VERSION );
        }
        else if ( $versionClient > $versionServer ) {
            $this->_responseError( E_PROTO_RESULT::CLIENT_NEW_VERSION );
        }
    }

    private function _responseError( $resultPotol, $message = '' ) {
        $this->_error = true;
        $this->_response->PROTO_RESULT = $resultPotol;
    }
    
    public function response(){
        $this->_response->returnJson();
    }
    
    // 检查参数合法性
    private function _checkArg() {
        if( $this->_error ) {
            return ;
        }
        
        $protoID = $this->_arg->get( Arg::PROTO );
        $platformID = $this->_arg->get( Arg::PLATFORM_ID );
    
        if ( E_PLATFORM::INVALID === $platformID ) {
            Logs::Err( '#PUBLISH_CODE_ISNOT_EXIST,CODE:' . $platformID );
            $this->_responseError( E_PROTO_RESULT::INVALID_DATA );
        }
    }
    
    // 检查维护状态
    private function _checkMaintenance() {
        if( $this->_error ) {
            return ;
        }
        
        if ( Env::Ins()->isMaintenance() ) {
            Logs::Debug( '#Maintenance' );
            $this->_responseError( E_PROTO_RESULT::SERVER_MAINTENANCE );
        }
    }

}